<template>
  <div id="Admin">
    <div v-if=this.have style="text-align: center">没有未处理的举报信息</div>

    <el-card shadow="hover" v-for="(item) in result" style="margin: 50px;">
      <el-link style="font-size: 20px" type="primary" @click.native="toArticle(item.article.id)">
        文章：{{ item.article.title }}
      </el-link>
      <div>作者：{{ item.article.authorName }}</div>
      <div>举报理由：{{ item.reason }}</div>
      <div style="text-align: right">
        <el-select v-model="value" placeholder="请选择封禁天数" style="width: 150px;margin-right: 10px">
          <el-option
              v-for="(days) in options"
              :key="days.value"
              :label="days.value"
              :value="days.value">
          </el-option>
        </el-select>
        <el-button type="success" icon="el-icon-check" circle @click="banThis(item.reportID)"></el-button>
        <el-button type="danger" icon="el-icon-close" circle @click="deleteMessage(item.reportID)"></el-button>
      </div>
      <div style="text-align: right">举报时间：{{ item.createTime }}</div>

    </el-card>
  </div>


</template>

<script>
import user from "@/store/user";

export default {
  name: "admin",
  data() {
    return {
      have: false,
      result: [],
      options: [{
        value: "0",
      }, {
        value: "1",
      }, {
        value: "3",
      }, {
        value: "5",
      }, {
        value: "7",
      }, {
        value: "14",
      }],
      value: ""
    }
  },
  created() {
    if (!this.$store.state.isAdmin) {
      window.alert("您不是管理员哦")
      this.$router.go(-1)
    }

    this.getAllReportedMessage()
  },
  methods: {
    banThis(id) {
      this.$axios({
        method: 'post',
        url: '/user/banUser',
        data: ({
          reportID:id,
          banTime:this.value,
          isBan:true
        })
      })
          .then(res => {
            if (res.data.code === 0)
              this.$message.success("封禁成功")
            location.reload()
          })
          .catch(err => {
            console.log(err);         /* 若出现异常则在终端输出相关信息 */
          })

    },
    deleteMessage(id){
      this.$axios({
        method: 'post',
        url: '/user/banUser',
        data: ({
          reportID:id,
          banTime:null,
          isBan:false
        })
      })
          .then(res => {
            if (res.data.code === 0)
              this.$message.success("删除成功")
            location.reload()
          })
          .catch(err => {
            console.log(err);         /* 若出现异常则在终端输出相关信息 */
          })
    },
    getAllReportedMessage: function () {
      this.$axios({
        method: 'post',
        url: '/user/manageReport',
        data: ({})
      })
          .then(res => {
            this.result = res.data.data
            if (this.result.length === 0)
              this.have = true;
            else this.have = false;
          })
          .catch(err => {
            console.log(err);         /* 若出现异常则在终端输出相关信息 */
          })
    },

    toArticle(id) {
      this.$router.push({path: '/text', query: {passageID: id}})
    }
  }
}
</script>


<style scoped>

</style>
